package com.example.tired1.zyfypt_14120200131_syn.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tired1.zyfypt_14120200131_syn.R;
import com.example.tired1.zyfypt_14120200131_syn.bean.ArticleBean;
import com.squareup.picasso.Picasso;


import java.util.List;

/**
 * Created by tired1 on 2017/4/18.
 */

public class Articleadapter extends RecyclerView.Adapter<Articleadapter.ViewHolder>{
    Context context;
    List<ArticleBean> list;
    public  Articleadapter(Context context, List<ArticleBean> list){
        this.context=context;
        this.list=list;

    }

    public  void  setList(List<ArticleBean> list){
        this.list=list;//更新数据时传到这里面
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ArticleBean bean=list.get(position);
        holder.tv_title.setText(bean.getTitle());
        holder.tv_descreption.setText(bean.getDescription());
        holder.tv_time.setText(bean.getTime());
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"+bean.getImgid())
                .placeholder(R.mipmap.ic_launcher)
                .into(holder.imageView);

    }

    @Override
    public int getItemCount() {
        if(list!=null)
            return  list.size();
        else
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_title, tv_time,tv_descreption;
        ImageView imageView;
        Button button;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title=(TextView)itemView.findViewById(R.id.tv_title);
            tv_descreption=(TextView)itemView.findViewById(R.id.tv_descreption);
            tv_time=(TextView)itemView.findViewById(R.id.tv_time);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
            button=(Button)itemView.findViewById(R.id.button);
        }
    }
}
